package com.mvvm.start.common.utils

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager


 /**
  * @功能: 软键盘工具类
  * @User Lmy
  * @Creat 2021/9/17 10:09 上午
  * @Compony 永远相信美好的事情即将发生
  */
object KeyboardUtils {
    /**
     * 自动关闭软键盘
     * @param activity
     */
    fun closeKeybord(activity: Activity) {
        val inputMethodManager =
                activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity.window?.decorView?.windowToken, 0)

    }

    /**
     * 打开关闭相互切换
     * @param activity
     */
    fun automaticKeyboard(activity: Activity) {
        val inputMethodManager =
                activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    /**
     * 强制显示软键盘 view为当前的输入框对象
     */
    fun showKeyboard(activity: Activity, view: View) {
        val inputMethodManager =
                activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

}
